package net.pws.oos.web;

import java.util.List;

import net.pws.common.jackson.JsonUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.Task;
import net.pws.oos.biz.query.TaskFinder;
import net.pws.oos.web.dto.TaskDto;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/biz/open")
public class OpenAction {
    
    public final transient Log logger = LogFactory.getLog(OpenAction.class);

    @Autowired
    private EntityManager entityManager;
    
    @Autowired
    private TaskFinder taskFinder;
    
    @Autowired
    private TaskAction taskAction;
    
    @RequestMapping("/map/task/detail.do")
    public String showTaskInMap(String id, ModelMap map) {
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("请提供任务ID");
        }
        Task task = taskFinder.getTask(id);
        if (null == task) {
            throw new RuntimeException("无效的任务ID");
        }
        map.put("task", JsonUtils.toJson(TaskDto.from(task)));
        return "open/map/task/index";
    }

    @RequestMapping("/map/task.do")
    public String showSingleTaskInMap(String id, ModelMap map) {
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("请提供任务ID");
        }
        Task task = taskFinder.getTask(id);
        if (null == task) {
            throw new RuntimeException("无效的任务ID");
        }
        map.put("task", JsonUtils.toJson(TaskDto.from(task)));
        return "open/map/task/index";
    }

    @RequestMapping("/map/path/task.do")
    public String showTaskPathInMap(String id, ModelMap map) {
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("请提供任务ID");
        }
        List<Task> taskList = findTaskByParent(id);
        map.put("tasks", JsonUtils.toJson(TaskDto.from(taskList)));
        return "open/map/task/path";
    }

    @RequestMapping("/map/project.do")
    public String showTasksOfProjectInMap(String id, ModelMap map) {
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("请提供项目ID");
        }
        List<Task> taskList = findTaskByProject(id);
        map.put("tasks", JsonUtils.toJson(TaskDto.from(taskList)));
        return "open/map/project/index";
    }
    
    public List<Task> findTaskByProject(String id) {
        return entityManager.findList("from Task t where t.project.id = ?", new Object[] {id});
    }

    public List<Task> findTaskByParent(String id) {
        return entityManager.findList("from Task t where t.parent.id = ?", new Object[] {id});
    }
    
    // delegate to taskAction
    
    @RequestMapping("/task/detail.do")
    public String showTaskDetail(String id, ModelMap map) {
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("请提供任务ID");
        }
        
        Task task = taskFinder.getTask(id);
        if (null == task) {
            throw new RuntimeException("无效的任务ID");
        }
        map.put("task", TaskDto.from(task));
        return "open/task/detail";
    }
    
    @RequestMapping("/task/detail.json")
    @ResponseBody
    public Object getTaskDetail(String id) {
        return taskAction.getTaskDetail(id);
    }
    
    @RequestMapping("/task/result/detail.json")
    @ResponseBody
    public Object getTaskResult(String taskId) {
        return taskAction.getTaskDetail(taskId);
    }
    
    @RequestMapping("/task/attachment/list.json")
    @ResponseBody
    public Object getAttachmentList(String resourceId) {
        return taskAction.getAttachmentList(resourceId);
    }
    
    @RequestMapping("/task/sub/list.json")
    @ResponseBody
    public Object getSubTaskList(String id) {
        //return taskAction.getSubTaskList(id);
    	return null;
    }

    @RequestMapping("/task/sub/count.json")
    @ResponseBody
    public Object getSubTaskCount(String id) {
        //return taskAction.getSubTaskCount(id);
    	return null;
    }
}
